/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifdef OHOS_LITE
#include "test_player_old.h"

#include <iostream>
#include <thread>
#include <chrono>

#include "scene/lite/hiplayer.h"
#include "foundation/log.h"

using namespace OHOS::Media;

namespace OHOS::Media::Test {
static bool g_playFinishedOld = false;

class PlayerCallbackImplOld : public PlayerCallback {
    void OnPlaybackComplete() override
    {
        g_playFinishedOld = true;
    }

    void OnError(int32_t errorType, int32_t errorCode) override
    {
    }

    void OnInfo(int type, int extra) override
    {
    }

    void OnVideoSizeChanged(int width, int height) override
    {
    }

    void OnRewindToComplete() override
    {
    }
};

bool StartPlayer(const std::string& url)
{
    std::cout << "Use media_lite interface player." << std::endl;
    g_playFinishedOld = false;
    auto player = OHOS::Media::CreateHiPlayer();
    FALSE_RETURN_V(0 == player->Init(), false);
    auto callback = std::make_shared<PlayerCallbackImplOld>();
    player->SetPlayerCallback(callback);
    OHOS::Media::Source source(url);
    FALSE_RETURN_V(0 == player->SetSource(source), false);
    FALSE_RETURN_V(0 == player->SetLoop(false), false);
    FALSE_RETURN_V(0 == player->Prepare(), false);
    FALSE_RETURN_V(0 == player->Play(), false);
    while (!g_playFinishedOld) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    FALSE_RETURN_V(g_playFinishedOld, false);
    return true;
}
}
#endif